package com.git.dengchongshuang.managementsystem;

import com.git.dengchongshuang.managementsystem.menu.BackException;
import com.git.dengchongshuang.managementsystem.menu.Menu;

import java.util.List;
import java.util.Scanner;

public class AddressBook {
    //主菜单功能类
    // private MainMenu mainMenu;
    //可以进行的操作类
    private OperateContacts operateContacts;
    //显示功能类
    private static final Scanner sc = new Scanner(System.in);
    private static final Menu mainMenu = new Menu(50);
    private static final Menu addMenu = new Menu(30);
    private static final Menu updateMenu = new Menu(30);
    private static final Menu updateSubMenu = new Menu(30);
    private static final Menu searchMenu = new Menu(30);
    private static boolean updateSubMenuInited = false;

    public static void main(String[] args) {
        //调用 mainMenu、operation、view 完成通讯录
        try {
            mainMenu();
            initUpdateMenu();
            searchMenu();
        } catch (Exception e) {

        }
        while (true) {
            mainMenu.showMenu();
            try {
                mainMenu.userInput("请输入选项: ");
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

        }

    }

    public static void mainMenu() {
        mainMenu.addMenuTitle("****************通讯录***************");
        mainMenu.addMenuItem("退出", "quit", () -> {
            System.out.println("bye~");
            System.exit(0);
        });
        mainMenu.addMenuItem("显示所有联系人", "all", () -> {
            View.showAllContactOrderByAddTime(true);
        });
        mainMenu.addMenuItem("添加联系人", "add", () -> {
            Contact contact = new Contact();
            addContactSetup(contact);
            OperateContacts.add(contact);
        });
        mainMenu.addMenuItem("修改联系人","update",()->{
            updateMenu.showMenuAndWaitForInput("请输入选项: ");
        });
        mainMenu.addMenuItem("查询联系人","search",()->{
            searchMenu.showMenuAndWaitForInput("请输入选项: ");
        });
    }

    public static void addContactSetup(Contact contact) {
        contact.setName(Menu.input("请输入姓名"));

        System.out.println("请输入电话：");
        String phoneNumber = sc.nextLine();
        contact.setPhoneNumber(phoneNumber);

        System.out.println("请输入邮件：");
        String email = sc.nextLine();
        contact.setEmail(email);

        System.out.println("请输入工作：");
        String jop = sc.nextLine();
        contact.setJob(jop);

        System.out.println("请输入公司：");
        String company = sc.nextLine();
        contact.setCompany(company);
    }

    // 修改单个联系人
    public static void showUpdateContactSubMenu(Contact contact) throws CloneNotSupportedException {
        Contact newCon = contact.clone();
        final String name = contact.getName();
        final String phoneNumber = contact.getPhoneNumber();
        if (!updateSubMenuInited) {
            updateSubMenu.addMenuTitle("***修改联系人***");
            updateSubMenu.addMenuItem("姓名", "n", () -> {
                newCon.setName(Menu.input("请输入新的姓名:"));
            });
            updateSubMenu.addMenuItem("电话", "p", () -> {
                newCon.setPhoneNumber(Menu.input("请输入新的电话:"));
            });
            updateSubMenu.addMenuItem("邮件", "e", () -> {
                newCon.setEmail(Menu.input("请输入新的邮件:"));
            });
            updateSubMenu.addMenuItem("工作", "j", () -> {
                newCon.setJob(Menu.input("请输入新的工作:"));
            });
            updateSubMenu.addMenuItem("公司", "c", () -> {
                newCon.setCompany(Menu.input("请输入新的公司:"));
            });
            updateSubMenu.addMenuItem("保存", "s", () -> {
                OperateContacts.updateContactByNameAndPhoneNumber(name, phoneNumber, newCon);
            });
            updateSubMenu.addMenuItem("返回", "b", () -> {
                throw new BackException();
            });
            updateSubMenuInited = true;
        }

        updateSubMenu.showMenuAndWaitForInput("请输入选项");
    }

    public static void initUpdateMenu() throws CloneNotSupportedException {
        updateMenu.addMenuTitle("***修改联系人***");
        updateMenu.addMenuTitle("请选择要修改的联系人的查找方式");
        updateMenu.addMenuItem("姓名", "name", () -> {
            String name = Menu.input("请输入要修改的联系人姓名");
            List<Contact> contacts = OperateContacts.getContactByName(name);
            if (contacts.size() == 1) {
                showUpdateContactSubMenu(contacts.get(0));
            } else if (contacts.size() > 1) {
                Menu menu = new Menu(40);
                for (int i = 0; i < contacts.size(); i++) {
                    Contact curCon = contacts.get(i);
                    menu.addMenuItem(curCon.getPhoneNumber(), String.valueOf(i), () -> {
                        showUpdateContactSubMenu(curCon);
                    });
                }

                menu.showMenuAndWaitForInput("请输入选项:");
            } else {
                System.out.println("没有联系人");
            }
        });
        updateMenu.addMenuItem("返回", "back", () -> {
            throw new BackException();
        });

    }

    public static void searchMenu() throws CloneNotSupportedException {
        searchMenu.addMenuTitle("***查找联系人***");
        searchMenu.addMenuTitle("请选择要修改的联系人的查找方式");

        searchMenu.addMenuItem("返回", "back", () -> {
            throw new BackException();
        });
    }

    private static String replaceEnter(String input) {
        if (input.equals("\n")) {
            return "";
        }
        return input;
    }
}

